package com.model.mapparser;

import com.core.RequestParams;
import com.model.project.ProjectTask;
import com.model.project.Version;
import com.model.project.attributes.TaskPriority;
import com.model.project.attributes.TaskResolution;
import com.model.project.attributes.TaskStatus;
import com.model.project.attributes.TaskType;
import com.model.user.User;
import com.servicelayer.project.VersionListLayer;
import com.servicelayer.project.attributes.TaskPriorityServiceLayer;
import com.servicelayer.project.attributes.TaskResolutionServiceLayer;
import com.servicelayer.project.attributes.TaskStatusServiceLayer;
import com.servicelayer.project.attributes.TaskTypeServiceLayer;
import com.servicelayer.user.UserServiceLayer;


public class ProjectTaskMapParser {

    public static ProjectTask fromMap ( RequestParams map ) {

        if ( map == null ) {
            return null;
        }

        Integer taskTypeId = map.integerAtIndexForKey(0, "taskType");

        Integer taskPriorityId = map.integerAtIndexForKey(0, "taskPriority");

        Integer taskStatusId = map.integerAtIndexForKey(0, "taskStatus");

        Integer taskResolutionId = map.integerAtIndexForKey(0, "taskResolution");

        Integer reporterId = map.integerAtIndexForKey(0, "reporter");

        Integer assigneeId = map.integerAtIndexForKey(0, "assignee");

        Integer fixVersionId = map.integerAtIndexForKey(0, "fixVersion");

        Integer affectVersionId = map.integerAtIndexForKey(0, "affectVersion");

        String name = map.stringAtIndexForKey(0, "name");

        String description = map.stringAtIndexForKey(0, "description");

        TaskTypeServiceLayer taskTypeServiceLayer = new TaskTypeServiceLayer();
        TaskType taskType = (TaskType) taskTypeServiceLayer.objectByUNID(taskTypeId, null, null);

        TaskPriorityServiceLayer taskPriorityServiceLayer = new TaskPriorityServiceLayer();
        TaskPriority taskPriority = (TaskPriority) taskPriorityServiceLayer.objectByUNID(taskPriorityId, null, null);

        TaskStatusServiceLayer taskStatusServiceLayer = new TaskStatusServiceLayer();
        TaskStatus taskStatus = (TaskStatus) taskStatusServiceLayer.objectByUNID(taskStatusId, null, null);

        TaskResolutionServiceLayer taskResolutionServiceLayer = new TaskResolutionServiceLayer();
        TaskResolution taskResolution = (TaskResolution) taskResolutionServiceLayer.objectByUNID(taskResolutionId, null, null);

        UserServiceLayer userServiceLayer = new UserServiceLayer();
        User taskAssignee = (User) userServiceLayer.objectByUNID(assigneeId, null, null);
        User taskReporter = (User) userServiceLayer.objectByUNID(reporterId, null, null);

        VersionListLayer versionServiceLayer = new VersionListLayer();
        Version fixVersion = (Version) versionServiceLayer.objectByUNID(fixVersionId, null, null);
        Version affectVersion = (Version) versionServiceLayer.objectByUNID(affectVersionId, null, null);

        ProjectTask template = null;
        template = new ProjectTask();
        template.setName(name);
        template.setDescription(description);
        template.setTaskType(taskType);
        template.setTaskPriority(taskPriority);
        template.setTaskStatus(taskStatus);
        template.setTaskResolution(taskResolution);
        template.setAssignee(taskAssignee);
        template.setReporter(taskReporter);
        template.setFixVersion(fixVersion);
        template.setAffectVersion(affectVersion);

        return template;

    }

}
